package com.bjsxt.front.portal.service.impl;

import com.bjsxt.front.portal.feign.CommonFeignClient;
import com.bjsxt.front.portal.feign.CommonRedisFeignClient;
import com.bjsxt.front.portal.service.ItemCategoryService;
import com.bjsxt.utils.CatResult;
import com.bjsxt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {

    @Autowired
    private CommonFeignClient commonFeignClient;

    @Autowired
    private CommonRedisFeignClient commonRedisFeignClient;

    @Override
    public Result selectItemCategoryAll() {
        //查询缓存
        try {
            CatResult catResult = this.commonRedisFeignClient.selectItemCategory();
            //判断缓存是否命中
            if(catResult!=null && catResult.getData()!=null && catResult.getData().size()>0){
                return Result.ok(catResult);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //查询数据库
        CatResult catResult = this.commonFeignClient.selectItemCategoryAll();
        try {
            if(catResult!=null && catResult.getData()!=null && catResult.getData().size()>0){
                this.commonRedisFeignClient.insertItemCategory(catResult);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        if(catResult!=null && catResult.getData()!=null && catResult.getData().size()>0){
            return Result.ok(catResult);
        }
        return Result.error("查无结果");
    }
}
